package webapi

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"iot-base/common/logger"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func InitStaticResource(r *gin.Engine) {
	file, _ := exec.LookPath(os.Args[0])
	path, err := filepath.Abs(file)
	if err != nil {
		logger.SLog.Error("error while abs", err)
		return
	}
	dir := filepath.Dir(path)
	if !strings.Contains(dir, "bin") {
		dir = dir + "/bin"
	}

	dir = dir + "/dist"
	fmt.Println("--------------------", dir)

	r.StaticFS("/static", http.Dir(dir))
	r.Static("/js", dir+"/js")
	r.Static("/css", dir+"/css")
	r.Static("/img", dir+"/img")
	r.Static("/assets", dir+"/assets")
	r.LoadHTMLFiles(dir + "/index.html")
	r.GET("/", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", nil)
	})
	r.StaticFS("/ui/", http.Dir(dir))
}
